package com.dunqi.gpm.cangxi.home.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatSpinner;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseAct;
import com.dunqi.gpm.cangxi.common.base.Data;
import com.dunqi.gpm.cangxi.common.util.TimeUtil;
import com.dunqi.gpm.cangxi.common.view.ComTitleBar;
import com.dunqi.gpm.cangxi.home.adapter.SpinnerAdapter;
import com.dunqi.gpm.cangxi.home.eventbus.SolveProblemEvent;
import com.dunqi.gpm.cangxi.home.widget.DatePickerDiaglog;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

public class SolveProblemActivity extends BaseAct {

    private static final String PROJECTPROBLEMID = "projectProblemId";
    private static final String PROBLEMSTATUS = "problemStatus";
    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    @BindView(R.id.spinner)
    AppCompatSpinner spinner;
    @BindView(R.id.time_tv)
    TextView timeTv;
    @BindView(R.id.attach_branch_edit)
    EditText attachBranchEdit;
    @BindView(R.id.solve_des_edit)
    EditText solveDesEdit;
    @BindView(R.id.container)
    View container;
    private long projectProblemId;
    private int problemStatus;
    private String time;

    String[] problemStatusTexts = {"请选择", "未处理", "已收集", "协调中", "已解决", "不能解决", "关闭"};

    @Override
    protected void refreshData(Bundle savedInstanceState) {

    }

    @Override
    protected void initUiAndListener() {
        Intent intent = getIntent();
        projectProblemId = intent.getLongExtra(PROJECTPROBLEMID, -1);
        problemStatus = intent.getIntExtra(PROBLEMSTATUS, 0);

//        if (HomeFragment.type == 18) {
        container.setVisibility(View.GONE);
//        } else {
//            container.setVisibility(View.VISIBLE);
//        }

        spinner.setAdapter(new SpinnerAdapter(this, problemStatusTexts));
        spinner.setSelection(problemStatus);

        time = TimeUtil.getCurrentTimeTen();
        timeTv.setText(time);

        titleBar.setOnClickListenerOfRight(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commit();
            }
        });

    }


    private void commit() {
        if (projectProblemId == -1) {
            t("此问题不存在");
            return;
        }
        int selectedItemPosition = spinner.getSelectedItemPosition();

        if (selectedItemPosition == 0) {
            t("请选择问题类型");
            return;
        }

        String attachBranch = getAttachBranch();
        String solveDes = getSolveDes();


        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("projectProblemId", String.valueOf(projectProblemId));
        hashMap.put("problemStatus", String.valueOf(selectedItemPosition));
        hashMap.put("coordinationTime", time);
        hashMap.put("coordinationSolutionDesc", solveDes);
//        if (HomeFragment.type == 18) {
//
//        } else {
//            hashMap.put("coordinationOrg", attachBranch);
//        }

        final ProgressDialog dialog = ProgressDialog.show(this, "提示", "正在提交...");
        ApiFactory.getInstance()
                .submitProjectProblemResolve(hashMap)
                .compose(RxSchedulers.<Data>io_main())
                .subscribe(new Consumer<Data>() {
                    @Override
                    public void accept(Data data) throws Exception {
                        setResult(607);
                        finish();
                        EventBus.getDefault().post(new SolveProblemEvent());
                        t("提交成功");
                        dialog.dismiss();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        t("提交失败");
                        dialog.dismiss();
                    }
                });

    }

    private String getSolveDes() {
        String solveDes = solveDesEdit.getText().toString().trim();
        return solveDes;
    }


    private String getAttachBranch() {
        String attachBranch = attachBranchEdit.getText().toString().trim();
        return attachBranch;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_solve_problem;
    }


    public static void start(Activity activity, long projectProblemId, int problemStatus) {
        Intent intent = new Intent(activity, SolveProblemActivity.class);
        intent.putExtra(PROJECTPROBLEMID, projectProblemId);
        intent.putExtra(PROBLEMSTATUS, problemStatus);
        activity.startActivityForResult(intent, 21);

    }


    @OnClick(R.id.time_tv)
    public void onViewClicked() {
        final DatePickerDiaglog diaglog1 = new DatePickerDiaglog(getActivity());
        diaglog1.show();
        diaglog1.setCanceledOnTouchOutside(true);
        diaglog1.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                time = diaglog1.getTime();
                timeTv.setText(time);
            }
        });
    }
}
